package com.nuo.common.excel.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.nuo.modules.student.entity.StudentEntity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface BaseIService<T> extends IService<T> {


    void saveOrUpdateBatch(List<T> list, Class<T> clazz);

    default void saveOrUpdateBatch(List<T> list, String column, String filed, Class<T> clazz) {
        try {
            Field f = clazz.getDeclaredField(filed);
            Field id = clazz.getDeclaredField("id");
            f.setAccessible(true);
            id.setAccessible(true);
            Map<String, T> map = new HashMap<>(100);
            for (T t : list) {
                map.put(f.get(t).toString(), t);
            }
            List<T> ts = list(new QueryWrapper<T>().in(column, map.keySet()));
            for (T t : ts) {
                id.set(map.get(f.get(t).toString()), id.get(t));
            }
            saveOrUpdateBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
